package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edu.domain.EduCourseCollect;
import com.ruoyi.edu.service.IEduCourseCollectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程收藏Controller
 * 
 * @author niruichen
 * @date 2020-12-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/edu/collect" )
public class EduCourseCollectController extends BaseController {

    private final IEduCourseCollectService iEduCourseCollectService;

    /**
     * 查询课程收藏列表
     */
    @PreAuthorize("@ss.hasPermi('edu:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduCourseCollect eduCourseCollect) {
        startPage();
        List<EduCourseCollect> list = iEduCourseCollectService.queryList(eduCourseCollect);
        return getDataTable(list);
    }

    /**
     * 导出课程收藏列表
     */
    @PreAuthorize("@ss.hasPermi('edu:collect:export')" )
    @Log(title = "课程收藏" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(EduCourseCollect eduCourseCollect) {
        List<EduCourseCollect> list = iEduCourseCollectService.queryList(eduCourseCollect);
        ExcelUtil<EduCourseCollect> util = new ExcelUtil<EduCourseCollect>(EduCourseCollect.class);
        return util.exportExcel(list, "collect" );
    }

    /**
     * 获取课程收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('edu:collect:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iEduCourseCollectService.getById(id));
    }

    /**
     * 新增课程收藏
     */
    @PreAuthorize("@ss.hasPermi('edu:collect:add')" )
    @Log(title = "课程收藏" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduCourseCollect eduCourseCollect) {
        return toAjax(iEduCourseCollectService.save(eduCourseCollect) ? 1 : 0);
    }

    /**
     * 修改课程收藏
     */
    @PreAuthorize("@ss.hasPermi('edu:collect:edit')" )
    @Log(title = "课程收藏" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduCourseCollect eduCourseCollect) {
        return toAjax(iEduCourseCollectService.updateById(eduCourseCollect) ? 1 : 0);
    }

    /**
     * 删除课程收藏
     */
    @PreAuthorize("@ss.hasPermi('edu:collect:remove')" )
    @Log(title = "课程收藏" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iEduCourseCollectService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
